procedure CheckForStack;
begin
  if nPics=0 then begin
    PutMessage('This macro requires a stack.');
    exit;
  end;
  if nSlices=0 then begin
    PutMessage('This window is not a stack.');
    exit
  end;
end;


procedure CheckForSelection;
var 
  x1,y1,x2,y2,LineWidth:integer;
begin
  GetRoi(RoiLeft,RoiTop,RoiWidth,RoiHeight);
  GetLine(x1,y1,x2,y2,LineWidth);
  if (RoiWidth=0) or (x1>=0) then begin
    PutMessage('Please make a rectangular selection.');
    exit;
  end;
end;


procedure ResliceMRI(srcHorizontal,dstHorizontal:boolean);
var
  stack1,stack2,width,height:integer;
  RoiLeft,RoiTop,RoiWidth,RoiHeight,max:integer;
  loc,PixelSpacing:real;
  InputSpacing,OutputSpacing:real; {mm}
  scale:real; {pixels/mm}  
  FirstTime:boolean;
begin
  scale:=1.0666; {Assumes 256x256 slices and 240mm field of view}
  RequiresVersion(1.45);
  CheckForStack;
  CheckForSelection;
  SaveState;
  SetScale(scale,'mm');
  SetBackground(0);
  SetBackground(255);
  stack1:=PicNumber;
  InputSpacing:=GetSliceSpacing/scale;
  if InputSpacing<=0 then InputSpacing:=1.5;
  InputSpacing:=GetNumber('Input Slice Spacing (mm):',InputSpacing);
  SetSliceSpacing(InputSpacing*scale);
  OutputSpacing:=InputSpacing;
  OutputSpacing:=GetNumber('Output Slice Spacing (mm):', OutputSpacing);
  PixelSpacing:=OutputSpacing*scale;
  FirstTime:=true;
  GetRoi(RoiLeft,RoiTop,RoiWidth,RoiHeight);
  if srcHorizontal then begin
    loc:=RoiTop+PixelSpacing;
    max:=RoiTop+RoiHeight;
  end else begin
    loc:=RoiLeft+PixelSpacing;
    max:=RoiLeft+RoiWidth;
  end;
  while loc<max do begin
    ChoosePic(stack1);
    if srcHorizontal
      then MakeLineRoi(RoiLeft,loc,RoiLeft+RoiWidth,loc)
      else MakeLineRoi(loc,RoiTop,loc,RoiTop+RoiTop+RoiHeight);
    if not dstHorizontal then SetOption;
    Reslice;
    SelectAll;
    Copy;
    GetPicSize(width,height);
    Dispose;
    if FirstTime then begin
      SetNewSize(width,height);
      MakeNewStack(OutputSpacing:1:2);
      SetSliceSpacing(PixelSpacing);
      stack2:=PicNumber;
    end;
    ChoosePic(stack2);
    if not FirstTime then AddSlice;
    Paste;
    loc:=loc+PixelSpacing;
    FirstTime:=false;
  end;
  SelectPic(stack1);
  KillRoi;
  SelectPic(stack2);
  KillRoi;
  RestoreState;
end;


macro 'Import GE Signa Files';
Var
  i,n,max,stack,first:integer;
  scale:real; {pixels/mm}
begin
  scale:=256 / 240; {assumes 256x256 slices with 240mm field of view}
  first:=round(GetNumber('Number of first slice:',1));
  max:=round(GetNumber('Maximum pixel value:',255));
  SetNewSize(256,256);
  MakeNewStack('Stack');
  stack:=nPics;
  MoveWindow(340,40);
  SetScale(scale,'mm');
  SetCustom(256,256,14336);
  SetImport('Custom; 16-bits Signed; Fixed Scale');
  SetImportMinMax(0,max);
  n:=first;
  for i:=1 to 256 do begin
    Import('i.',n:3);
    SetPicName('i.',n:3);
    SelectAll;
    Copy;
    Dispose;
    SelectPic(stack);
    if n<>first then AddSlice;
    n:=n+1;
    Paste;
   end;
end;


macro '(-' begin end;

macro 'Sagitals to Coronals'; begin ResliceMRI(false,false) end;
macro 'Sagitals to Axials'; begin ResliceMRI(true,false) end;
macro 'Coronals to Sagitals'; begin ResliceMRI(false,false) end;
macro 'Coronals to Axials'; begin ResliceMRI(true,true) end;
macro 'Axials to Coronals'; begin ResliceMRI(true, true) end;
macro 'Axials to Sagitals'; begin ResliceMRI(false, true) end;


macro '(-' begin end;

procedure flip(vertical:boolean);
var
  i:integer;
  SliceSpacing:real;
begin
  CheckForStack;
  for i:= 1 to nSlices do begin
    SelectSlice(i);
    if vertical
      then FlipVertical
      else FlipHorizontal;
  end;
end;

macro 'Flip Vertical';   begin flip(true) end;
macro 'Flip Horizontal'; begin flip(false) end;
